import { Component, OnInit , Input,Output,OnChanges,SimpleChanges } from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import { TreeService } from 'src/pages/services/tree.service';
import {HttpClient,HttpHeaders,HttpParams} from '@angular/common/http';
import {DragdropService} from '../../services/dragdrop.service'

declare var $: any;
import * as _ from 'underscore';

@Component({
  selector: 'app-schema',
  templateUrl: './schema.component.html',
  styleUrls: ['./schema.component.css']
})
export class SchemaComponent implements OnInit,OnChanges{

  curWidget={}
  columnObjs=[];   //向modal传递数据集字段
  keys=[]
  //模拟维度列数据
  dimenColumn={};
  dialogshow:boolean;    //modal-dialog控制显示隐藏
  
  private setdataid:any;
  private index:any;
  private initItem:any;     //编辑过滤器传的值
  private editevent:any;    //点击过滤传递编辑事件
  private isDsFilter=false;
  private dragarray1:any;    //保存拖动的数组
  private dragarray2:any;
  private backdrop:any;     //接收被删除的按钮
  private dropstype:any;    //保存被拖动行的type
  private filterarray:any;   //保存过滤的数组
  private filterindex:any    //保存过滤删除的序号
  private measurearray:any;
  private measureindex:any;
  
  dimensionShow:boolean=true;

  customDs=false;        //可选表达式+图标

  constructor(
    public translate:TranslateService,
    private treeservice:TreeService,
    private dragdropservice:DragdropService
  ) {
    translate.addLangs(['zh','en']);                          //添加语言支持
    translate.setDefaultLang('en');                           //设置默认语言
    let broswerLang = translate.getBrowserLang();              //获取浏览器支持的语言
    console.log(broswerLang)                                   //zh
    translate.use(broswerLang.match(/zh|en/) ? broswerLang : 'en');

    
    this.dragdropservice.dropEl.subscribe((result)=>{
      console.log(result)
      if(result.other=='a'){
        this.dragarray1.splice(this.index,0,result)
      }
      if(result.other=='b'){
        this.dragarray2.splice(this.index,0,result)
      }
    })

    this.dragdropservice.filterEl.subscribe((result)=>{
      console.log(result)
      this.filterarray.splice(this.filterindex,0,result)
    })
    
   }

  ngOnInit() {
    this.getshujuji();
  }

  getshujuji(){
    this.treeservice.getdataName().then((data)=>{
      this.setdataid=data
      this.treeservice.getDataSet(this.setdataid).then((res)=>{
        console.log(res.data)
        this.dimenColumn['dimension']=res.data.schema.dimension
        this.dimenColumn["measure"]=res.data.schema.measure
        this.dimenColumn['selects']=res.data.selects
        this.curWidget['expressions']=res.data.expressions;
        this.curWidget['filterGroups']=res.data.filters

        let dimensions = res.data.schema.dimension
        let measures = res.data.schema.measure
        for(let i=0;i<dimensions.length;i++){
          this.columnObjs.push(dimensions[i])
        }
        for(let i=0;i<measures.length;i++){
          this.columnObjs.push(measures[i])
        }
        console.log(this.columnObjs)
        // console.log(this.curWidget)       
        //schema.dimension 是维度列   schema.measure是指标列 
        //schema.selects 是未分类列
        //expressions   可选表达式
       
      })
    })
    console.log(this.dimenColumn)
    console.log(this.curWidget)
  }

  ngOnChanges(changes:SimpleChanges){
    
  }
  
  dimension(){
    this.dimensionShow=!this.dimensionShow
  }
  editExp (col) {
    
  }

    public isDsExpression (o) {
      
  };

  editFilterGroup(item,ev){
    this.dialogshow=true;
    console.log(ev)
    if(item){
      this.initItem=item.group;
      console.log(this.initItem)
      this.editevent=ev
    }
  }
  revise(ev){
    console.log(ev)
    this.dialogshow=ev
  }

  viewExp (exp) {
    // ModalUtils.alert({title: this.translate.instant('CONFIG.COMMON.CUSTOM_EXPRESSION') + ': ' + exp.alias, body: exp.exp},
    //     "modal-info", 'lg');
}

//获取别名
getalias(data){
  this.curWidget["alias"]=data
  console.log(this.curWidget['alias'])
}
aliasevent(ev){
  if(ev){
    this.isDsFilter=true
  }
}
delete(){
  this.curWidget['alias']=''
  this.isDsFilter=false
}

public onDragCancle (el1,el2) {
  for(let i=0;i<el2.length;i++){
    if(el1==el2[i].group){
      this.filterindex=i;
      console.log(this.filterindex)
      el2.splice(this.index,1)
    }
  }
}

measuredragend(el,array){
  for(let i=0;i<array.length;i++){
    if(el==array[i].column){
      this.measureindex=i;
      console.log(this.measureindex)
      array.splice(this.measureindex,1)
    }
  }
}
measureDragstart(el,array){
  console.log(el)
  this.measurearray=array;
  this.dragdropservice.setmeasuredrag(el,array)
}

  public dragstart (el1,style,all) {
    console.log(el1)
    this.dragdropservice.setdrag(el1,style,all);
  };
  dragstartcol(el1,style,all){
    console.log(el1)
    this.dragdropservice.setdragcol(el1,style,all)
  }

  //拖动以后删除一个元素
  dragend1(el1,el2,type){
    console.log(el1)
    console.log(el2)   
    console.log(type)
    this.dropstype=type
    this.dragarray1=el2      
    for(let i=0;i<el2.length;i++){
      if(el1==el2[i].column){
        this.index=i;
        console.log(this.index)
        el2.splice(this.index,1)
      }
    }
    console.log(el2)
  }

  dragend2(el1,el2,type){
    console.log(el1)
    console.log(el2)   
    console.log(type)
    this.dropstype=type
    this.dragarray2=el2      
    for(let i=0;i<el2.length;i++){
      if(el1==el2[i].column){
        this.index=i;
        console.log(this.index)
        el2.splice(this.index,1)
      }
    }
  }

  onDragstart(el,array){
    console.log(el)
    console.log(array)
    this.filterarray=array;
    this.dragdropservice.filtersetdrag(el,array);

  }
 
}
